package com.eras.shoes.adapter

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.eras.shoes.R
import com.eras.shoes.contents.BleBean
import com.eras.shoes.contents.Global

class DeviceAdapter(private val onClick: (BleBean) -> Unit) :
    ListAdapter<BleBean, DeviceAdapter.ViewHolder>(DeviceDiffCallback()) {
    private val connectedDevices = mutableSetOf<String>()
    private val devices = mutableListOf<BleBean>()
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        @SuppressLint("MissingPermission", "SetTextI18n")
        fun bind(device: BleBean) {
            itemView.apply {
                findViewById<TextView>(R.id.tvDeviceName).text = device.name ?: "未知设备"
                findViewById<TextView>(R.id.tvDeviceAddress).text =
                    device.address + "  " + device.serialNumber + "  " + device.rssi
                if (device.connectState == -1) {
                    itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.GONE
                    itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.GONE
                } else if (device.connectState == 0) {
                    itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.VISIBLE
                    itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.GONE
                } else if (device.connectState == 1) {
                    itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.GONE
                    itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.VISIBLE
                }
//                findViewById<ProgressBar>(R.id.pb_loading).visibility = if (device.connectState==0) {
//                    View.VISIBLE
//                } else {
//                    View.GONE
//                }
//                findViewById<ImageView>(R.id.iv_success).visibility = if (connectedDevices.contains(device.address)) {
//                    View.VISIBLE
//                } else {
//                    View.GONE
//                }
                setOnClickListener { onClick(device) }
            }


//            itemView.findViewById<TextView>(R.id.tvDeviceName).text = device.name ?: "未知设备"
//            itemView.findViewById<TextView>(R.id.tvDeviceAddress).text = device.address + "  " + device.serialNumber + "  " +device.rssi
//            itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.GONE;
//            itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.GONE
//            if (device.address == Global.address) {
//                when (Global.connectType) {
//                    -1 -> {
//                        itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.GONE
//                        itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.GONE
//                    }
//                    0 -> {
//                        itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.VISIBLE
//                        itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.GONE
//                    }
//                    1 -> {
//                        itemView.findViewById<ProgressBar>(R.id.pb_loading).visibility = View.GONE
//                        itemView.findViewById<ImageView>(R.id.iv_success).visibility = View.VISIBLE
//                    }
//
//                }
//            }

        }
    }
    fun addDevice(device: BleBean) {
        if (!devices.any { it.address == device.address }) {
            devices.add(device)
            notifyItemInserted(devices.size - 1)
        }
    }

    fun setConnectedDevices(device: BleBean) {
        connectedDevices.clear()
        connectedDevices.add(device.address)
//        device.forEach { connectedDevices.add(it.address) }
        notifyDataSetChanged()
    }

    fun clearConnectedDevices() {
        connectedDevices.clear()
        notifyDataSetChanged()
    }

    fun clearDevices() {
        devices.clear()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_device, parent, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
        holder.itemView.setOnClickListener { onClick(getItem(position)) }
    }

    class DeviceDiffCallback : DiffUtil.ItemCallback<BleBean>() {
        override fun areItemsTheSame(oldItem: BleBean, newItem: BleBean): Boolean {
            return oldItem.address == newItem.address
        }

        @SuppressLint("DiffUtilEquals")
        override fun areContentsTheSame(oldItem: BleBean, newItem: BleBean): Boolean {
            return oldItem.equals(newItem)
        }
    }
}
